﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using middleware.utiles;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace middleware.dominio
{
    [Table("Reglas")]
    public class Regla
    {
        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ReglaId { get; set; }
        public string nombreAtributo { get; set; }
        public string codicion { get; set; }
        public string valorAtributo { get; set; }

        [ForeignKey("bandeja")]
        public int BandejaId { get; set; }
        public BandejaSalida bandeja { get; set; }        

        public Boolean MensajeCumpleRegla(Mensaje mensaje)
        {
            Boolean result = false;

            foreach (Atributo atributo in mensaje.atributos)
            {                
                if (atributo.nombre.CompareTo(nombreAtributo) == 0)
                {
                    switch (codicion)
                    {
                        case "=":
                            if (String.Compare(atributo.valor, valorAtributo, StringComparison.OrdinalIgnoreCase) == 0)
                            {
                                result = true;
                            }
                            break;
                        case "<":
                            if (String.Compare(atributo.valor, valorAtributo, StringComparison.OrdinalIgnoreCase) < 0)
                            {
                                result = true;
                            }
                            break;
                        case ">":
                            if (String.Compare(atributo.valor, valorAtributo, StringComparison.OrdinalIgnoreCase) > 0)
                            {
                                result = true;
                            }
                            break;
                        case "<=":
                            if (String.Compare(atributo.valor, valorAtributo, StringComparison.OrdinalIgnoreCase) <= 0)
                            {
                                result = true;
                            }
                            break;
                        case ">=":
                            if (String.Compare(atributo.valor, valorAtributo, StringComparison.OrdinalIgnoreCase) >= 0)
                            {
                                result = true;
                            }
                            break;
                        case "<>":
                            if (String.Compare(atributo.valor, valorAtributo, StringComparison.OrdinalIgnoreCase) != 0)
                            {
                                result = true;
                            }
                            break;
                    }
                }
                if (result)
                {
                    break;
                }
            }

            return result;
        }

        override
        public string ToString()
        {
            return nombreAtributo + " " + codicion + " " + valorAtributo;
        }

    }
}
